using System;
using System.Xml.Linq;

namespace FluentlyXml.Core.Serialization
{
	/// <summary>
	/// Performs serialization of objects.  
	/// </summary>
	internal class FluentSerializer : IFluentSerializer
	{
		private readonly IRuntimeSerializationConfig _config;

		public FluentSerializer(IRuntimeSerializationConfig config)
		{
			_config = config;
		}

		/// <summary> 
		/// Serializes an object.
		/// </summary>
		public XElement Serialize<T>(T obj)
		{
			return Serialize(typeof (T), obj);
		}

		public XElement Serialize(Type type, object obj)
		{
			return SerializeInternal(type, obj, new SerializationContext(this));
		}

		internal XElement SerializeInternal(Type type, object obj, ISerializationContext context)
		{
			if (obj == null)
			{
				throw new ArgumentException("Object to serialize may not be null.", "obj");
			}

			var serializer = _config.GetSerializerFor(type);

			return serializer.Serialize(type, obj, context);
		}


		/// <summary>
		/// Clears the serializer's identity cache. 
		/// </summary>
		public void Dispose()
		{
		}
	}
}

